﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SaaS
{
    public partial class PublishService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);
                string serviceid = GridView1.DataKeys[index].Value.ToString();
                MiniSaaSDB db = new MiniSaaSDB();
                db.open();
                db.executeNonQuery("DELETE FROM SYSTEM_WORKFLOW_SERVICE WHERE Service_ID = " + serviceid + ";");
                db.executeNonQuery("DELETE FROM TENANT_WORKFLOW_SERVICE WHERE Service_ID = " + serviceid + ";");
                db.executeNonQuery("DELETE FROM SERVICE WHERE Service_ID = " + serviceid + ";");
                db.close();
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MiniSaaSDB db = new MiniSaaSDB();
            SqlDataReader reader;
            string nextID;
            db.open();
            reader = db.executeQuery("SELECT MAX(Service_ID)+1 FROM SERVICE;");
            if (!reader.HasRows){
                nextID="1";
            }
            else{
                reader.Read();
                if (reader[0] == null || reader[0].ToString().Equals(""))
                    nextID = "1";
                else
                    nextID = reader[0].ToString();
            }
            db.executeNonQuery("INSERT INTO SERVICE (Service_ID, Service_Name, " +
                "Service_Path, Service_Function_Name, Service_Description) VALUES (" + nextID.ToString() +
                ", '" + txtServiceName.Text.Trim() + "', '" + txtServicePath.Text.Trim() + "', '" +
                txtServiceFunction.Text.Trim() + "', '" + txtServiceDescription.Text.Trim() + "');");
            db.close();
            GridView1.DataBind();
        }

        protected void GridView1_DataBinding(object sender, EventArgs e)
        {

        }

        protected void SqlDataSource1_Deleting(object sender, SqlDataSourceCommandEventArgs e)
        {
        }
    }
}
